<template>
  <el-dialog :append-to-body="true" :visible.sync="dialog" :title="isAdd ? '新增模板信息' : '编辑模板信息'" width="700px" @close="cancel">
    <el-form ref="form" :model="form" :rules="rules" size="small" label-width="100px">
      <el-form-item label="模板名称" prop="template_name">
        <el-input v-model="form.template_name" style="width: 500px;"/>
      </el-form-item>
      <el-form-item label="短信签名" prop="sign_name">
        <el-input v-model="form.sign_name" style="width: 500px;"/>
      </el-form-item>
      <el-form-item label="模板内容" prop="template">
        <el-input v-model="form.template" style="width: 500px;" type="textarea"/>
      </el-form-item>
      <!--<el-form-item label="平台类型" prop="system_type">
        <el-select v-model="form.system_type" clearable placeholder="平台类型" class="filter-item" style="width: 500px">
          <el-option v-for="item in typeOptions" :key="item.value" :label="item.name" :value="item.value"/>
        </el-select>
      </el-form-item>-->
      <el-form-item label="模板类型" prop="template_type">
        <el-select v-model="form.template_type" clearable placeholder="模板类型" class="filter-item" style="width: 500px">
          <el-option v-for="item in dic_list.template_type" :key="item.dict_code" :label="item.label" :value="item.dict_code"/>
        </el-select>
      </el-form-item>
      <el-form-item label="发送对象" prop="object_type">
        <el-select v-model="form.object_type" clearable placeholder="发送对象" class="filter-item" style="width: 500px">
          <el-option v-for="item in dic_list.object_type" :key="item.dict_code" :label="item.label" :value="item.dict_code"/>
        </el-select>
      </el-form-item>
      <!--<el-form-item label="模板说明" prop="template_illustrate">
        <el-input v-model="form.template_illustrate" style="width: 500px;" type="textarea"/>
      </el-form-item>-->
      <el-form-item label="发送类型" prop="send_type">
        <el-select v-model="form.send_type" clearable placeholder="发送类型" class="filter-item" style="width: 500px">
          <el-option v-for="item in dic_list.send_type" :key="item.dict_code" :label="item.label" :value="item.dict_code"/>
        </el-select>
      </el-form-item>
      <el-form-item label="失败重发次数">
        <el-input-number v-model="form.re_count" :min="0" controls-position="right" style="width: 150px;"></el-input-number>
      </el-form-item>
      <el-form-item label="发送时间">
        <el-time-select
          v-model="form.send_time"
          :picker-options="{
            start: '06:00',
            step: '00:30',
            end: '18:00'
          }"
          style="width: 150px;"
          placeholder="选择时间">
        </el-time-select>
      </el-form-item>
      <el-form-item label="是否有效">
        <el-radio-group v-model="form.status">
          <el-radio :label="1">是</el-radio>
          <el-radio :label="0">否</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="danger" @click="cancel">取消</el-button>
      <el-button :loading="loading" type="primary" @click="doSubmit">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { add, edit } from '@/api/message/messageRecord'
export default {
  props: {
    isAdd: {
      type: Boolean,
      required: true
    },
    sup_this: {
      type: Object,
      default: null
    },
    dic_list: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      // typeOptions: [
      //   { value: '1', name: '验证码' },
      //   { value: '2', name: '短信通知' },
      //   { value: '3', name: '推广短信' }
      // ],
      content_word: [],
      loading: false, dialog: false,
      form: {},
      rules: {
        template_name: [
          { required: true, message: '请输入模板名称', trigger: 'blur' }
        ],
        sign_name: [
          { required: true, message: '请输入短信签名', trigger: 'blur' }
        ],
        template: [
          { required: true, message: '请输入模板内容', trigger: 'blur' }
        ],
        template_type: [
          { required: true, message: '请选择模板类型', trigger: 'blur' }
        ],
        object_type: [
          { required: true, message: '请选择发送对象', trigger: 'blur' }
        ],
        send_type: [
          { required: true, message: '请选择发送类型', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    cancel() {
      this.resetForm()
    },
    doSubmit() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true
          if (this.isAdd) {
            this.doAdd()
          } else this.doEdit()
        } else {
          return false
        }
      })
    },
    doAdd() {
      add(this.form).then(res => {
        this.resetForm()
        this.$message({
          showClose: true,
          type: 'success',
          message: '添加成功!',
          duration: 2500
        })
        this.loading = false
        this.$parent.$parent.init()
      }).catch(err => {
        this.loading = false
        console.log(err)
      })
    },
    doEdit() {
      edit(this.form.id, this.form).then(res => {
        this.resetForm()
        this.$message({
          showClose: true,
          type: 'success',
          message: '修改成功!',
          duration: 2500
        })
        this.loading = false
        this.sup_this.init()
      }).catch(err => {
        this.loading = false
        console.log(err)
      })
    },
    resetForm() {
      this.dialog = false
      this.$refs['form'].resetFields()
      this.form = {}
      this.content_word = []
    }
  }
}
</script>

<style scoped>

</style>
